unit GkxContxt;

interface
uses
  Classes,SysUtils,Uni,UniConnct,UniEngine,IniFiles,
  Class_CORD_IN_GKZF,Class_TYPE_IN_GKZF,Class_GKJG_FL_IN_GKZF;

type
  TGkxContxt=class(TUniEngine)
  private
    FListNode:TStringList;//*list of *tgkxCachNode.format('%S-%S',unitlink,object.classname);
  public
    //tcord
    procedure UpdateCord(AUniConnection:TUniConnection;IsForbit:Boolean=False;AUnitLink:string='-1');
    function  GetObjtCord(AIdex:Integer;IsCreate:Boolean=False;AUnitLink:string='-1'):TCORD;
  public
    //ttype
    procedure UpdateType(AUniConnection:TUniConnection;IsForbit:Boolean=False;AUnitLink:string='-1');
    function  GetObjtType(AIdex:Integer;AType:string;IsCreate:Boolean;AUnitLink:string='-1'):TTYPE;
    function  GetNameType(AIdex:Integer;AType:string;AUnitLink:string='-1'):string;
  public  
    //ttype.F
    procedure UpdateTypF(AUniConnection:TUniConnection;IsForbit:Boolean=False;AUnitLink:string='-1');
    function  GetObjtTypF(AText:string;AType:Integer;IsCreate:Boolean;AUnitLink:string='-1'):TTYPE;
    function  GetCodeTypF(AText:string;AType:Integer;AUnitLink:string='-1'):string;
    function  GetMemoTypF(AText:string;AType:Integer;AUnitLink:string='-1'):string;
  public
    //tgkjgfl
    procedure UpdateJGFL(AUniConnection:TUniConnection;IsForbit:Boolean=False;AUnitLink:string='-1');
    function  GetObjtJGFL(AFLID:Integer;IsCreate:Boolean=False;AUnitLink:string='-1';AGZJGIDEX:Integer=1):TGKJGFL;
  public
    constructor Create;
    destructor Destroy; override;
  end;

  TGkxCachNode=class(TObject)
  private
    FListData:THashedStringList;//*list of *tobject
  public
    //Tcord:unit_link-cord_idex
    procedure UpdateCord(AUniConnection:TUniConnection;IsForbit:Boolean=False;AUnitLink:string='-1');
    function  GetObjtCord(AIdex:Integer;IsCreate:Boolean=False;AUnitLink:string='-1'):TCORD;
  public
    //ttype:unit_link-cord_type-type_idex
    procedure UpdateType(AUniConnection:TUniConnection;IsForbit:Boolean=False;AUnitLink:string='-1');
    function  GetObjtType(AIdex:Integer;AType:string;IsCreate:Boolean;AUnitLink:string='-1'):TTYPE;
  public
    //ttype.F:unit_link-cord_idex-type_name
    procedure UpdateTypF(AUniConnection:TUniConnection;IsForbit:Boolean=False;AUnitLink:string='-1');
    function  GetObjtTypF(AText:string;AType:Integer;IsCreate:Boolean;AUnitLink:string='-1'):TTYPE;
  public
    //tgkjgfl
    procedure UpdateJGFL(AUniConnection:TUniConnection;IsForbit:Boolean=False;AUnitLink:string='-1');
    function  GetObjtJGFL(AFLID:Integer;IsCreate:Boolean=False;AUnitLink:string='-1';AGZJGIDEX:Integer=1):TGKJGFL;  
  public
    constructor Create;
    destructor Destroy; override;
  end;

var
  GkxContxtEx:TGkxContxt;

const
  CONST_FETCH_ERROR='<FETCH.ERROR>';

implementation

uses
  Class_KzUtils,Class_AppUtil;

{ TGkxContxt }

constructor TGkxContxt.Create;
begin
  FListNode:=TStringList.Create;
end;

destructor TGkxContxt.Destroy;
begin
  if FListNode<>nil then TKzUtils.TryFreeAndNil(FListNode);
  inherited;
end;

function TGkxContxt.GetCodeTypF(AText: string; AType: Integer;
  AUnitLink: string): string;
var
  TypeA:TTYPE;
begin
  Result:=CONST_FETCH_ERROR;
  TypeA:=nil;
  TypeA:=GetObjtTypF(AText,AType,False,AUnitLink);
  if TypeA<>nil then
  begin
    Result:=TypeA.TYPELINK;
  end;
end;

function TGkxContxt.GetMemoTypF(AText: string; AType: Integer;
  AUnitLink: string): string;
var
  TypeA:TTYPE;
begin
  Result:=CONST_FETCH_ERROR;
  TypeA:=nil;
  TypeA:=GetObjtTypF(AText,AType,False,AUnitLink);
  if TypeA<>nil then
  begin
    Result:=TypeA.TYPEMEMO;
  end;
end;

function TGkxContxt.GetNameType(AIdex: Integer; AType: string;AUnitLink: string): string;
var
  TypeA:TTYPE;
begin
  Result:=CONST_FETCH_ERROR;
  TypeA:=nil;
  TypeA:=GetObjtType(AIdex,AType,False,AUnitLink);
  if TypeA<>nil then
  begin
    Result:=TypeA.TYPENAME;
  end;
end;

function TGkxContxt.GetObjtCord(AIdex: Integer; IsCreate: Boolean;
  AUnitLink: string):TCORD;
var
  IdexA:Integer;
  TextA:string;
  CachNode:TGkxCachNode;  
begin
  Result:=nil;
  TextA:=Format('%S-%S',[AUnitLink,'TCORD']);
  IdexA:=-1;
  IdexA:=FListNode.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    CachNode:=nil;
    CachNode:=TGkxCachNode(FListNode.Objects[IdexA]);
    if CachNode<>nil then
    begin
      Result:=CachNode.GetObjtCord(AIdex,IsCreate,AUnitLink);
    end;
  end else
  begin
    CachNode:=TGkxCachNode.Create;
    Result:=CachNode.GetObjtCord(AIdex,IsCreate,AUnitLink);
    FListNode.AddObject(TextA,CachNode);
  end;
end;

function TGkxContxt.GetObjtJGFL(AFLID: Integer; IsCreate: Boolean;
  AUnitLink: string; AGZJGIDEX: Integer): TGKJGFL;
var
  IdexA:Integer;
  TextA:string;
  CachNode:TGkxCachNode;  
begin
  Result:=nil;
  TextA:=Format('%S-%S',[AUnitLink,'TGKJGFL']);
  IdexA:=-1;
  IdexA:=FListNode.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    CachNode:=nil;
    CachNode:=TGkxCachNode(FListNode.Objects[IdexA]);
    if CachNode<>nil then
    begin
      Result:=CachNode.GetObjtJGFL(AFLID,IsCreate,AUnitLink,AGZJGIDEX);
    end;
  end else
  begin
    CachNode:=TGkxCachNode.Create;
    Result:=CachNode.GetObjtJGFL(AFLID,IsCreate,AUnitLink,AGZJGIDEX);
    FListNode.AddObject(TextA,CachNode);
  end;
end;

function TGkxContxt.GetObjtType(AIdex: Integer; AType: string;
  IsCreate: Boolean; AUnitLink: string): TTYPE;
var
  IdexA:Integer;
  TextA:string;
  CachNode:TGkxCachNode;  
begin
  Result:=nil;
  TextA:=Format('%S-%S',[AUnitLink,'TTYPE']);
  IdexA:=-1;
  IdexA:=FListNode.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    CachNode:=nil;
    CachNode:=TGkxCachNode(FListNode.Objects[IdexA]);
    if CachNode<>nil then
    begin
      Result:=CachNode.GetObjtType(AIdex,AType,IsCreate,AUnitLink);
    end;
  end else
  begin
    CachNode:=TGkxCachNode.Create;
    Result:=CachNode.GetObjtType(AIdex,AType,IsCreate,AUnitLink);
    FListNode.AddObject(TextA,CachNode);
  end;
end;

function TGkxContxt.GetObjtTypF(AText:string;AType:Integer;
  IsCreate: Boolean; AUnitLink: string): TTYPE;
var
  IdexA:Integer;
  TextA:string;
  CachNode:TGkxCachNode;  
begin
  Result:=nil;
  TextA:=Format('%S-%S',[AUnitLink,'TTYPE.F']);
  IdexA:=-1;
  IdexA:=FListNode.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    CachNode:=nil;
    CachNode:=TGkxCachNode(FListNode.Objects[IdexA]);
    if CachNode<>nil then
    begin
      Result:=CachNode.GetObjtTypF(AText,AType,IsCreate,AUnitLink);
    end;
  end else
  begin
    CachNode:=TGkxCachNode.Create;
    Result:=CachNode.GetObjtTypF(AText,AType,IsCreate,AUnitLink);
    FListNode.AddObject(TextA,CachNode);
  end;
end;

procedure TGkxContxt.UpdateCord(AUniConnection: TUniConnection;
  IsForbit: Boolean; AUnitLink: string);
var
  IdexA:Integer;
  TextA:string;
  CachNode:TGkxCachNode;  
begin
  TextA:=Format('%S-%S',[AUnitLink,'TCORD']);
  IdexA:=-1;
  IdexA:=FListNode.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    CachNode:=nil;
    CachNode:=TGkxCachNode(FListNode.Objects[IdexA]);
    if CachNode<>nil then
    begin
      if IsForbit then
      begin
        CachNode.UpdateCord(AUniConnection,IsForbit,AUnitLink);
      end;
    end;
  end else
  begin
    CachNode:=TGkxCachNode.Create;
    CachNode.UpdateCord(AUniConnection,IsForbit,AUnitLink);
    FListNode.AddObject(TextA,CachNode);
  end;
end;

procedure TGkxContxt.UpdateJGFL(AUniConnection: TUniConnection;
  IsForbit: Boolean; AUnitLink: string);
var
  IdexA:Integer;
  TextA:string;
  CachNode:TGkxCachNode;  
begin
  TextA:=Format('%S-%S',[AUnitLink,'TGKJGFL']);
  IdexA:=-1;
  IdexA:=FListNode.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    CachNode:=nil;
    CachNode:=TGkxCachNode(FListNode.Objects[IdexA]);
    if CachNode<>nil then
    begin
      if IsForbit then
      begin
        CachNode.UpdateJGFL(AUniConnection,IsForbit,AUnitLink);
      end;
    end;
  end else
  begin
    CachNode:=TGkxCachNode.Create;
    CachNode.UpdateJGFL(AUniConnection,IsForbit,AUnitLink);
    FListNode.AddObject(TextA,CachNode);
  end;
end;

procedure TGkxContxt.UpdateType(AUniConnection: TUniConnection;
  IsForbit: Boolean; AUnitLink: string);
var
  IdexA:Integer;
  TextA:string;
  CachNode:TGkxCachNode;  
begin
  TextA:=Format('%S-%S',[AUnitLink,'TTYPE']);
  IdexA:=-1;
  IdexA:=FListNode.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    CachNode:=nil;
    CachNode:=TGkxCachNode(FListNode.Objects[IdexA]);
    if CachNode<>nil then
    begin
      if IsForbit then
      begin
        CachNode.UpdateType(AUniConnection,IsForbit,AUnitLink);
      end;
    end;
  end else
  begin
    CachNode:=TGkxCachNode.Create;
    CachNode.UpdateType(AUniConnection,IsForbit,AUnitLink);
    FListNode.AddObject(TextA,CachNode);
  end;
end;

procedure TGkxContxt.UpdateTypF(AUniConnection: TUniConnection;
  IsForbit: Boolean; AUnitLink: string);
var
  IdexA:Integer;
  TextA:string;
  CachNode:TGkxCachNode;  
begin
  TextA:=Format('%S-%S',[AUnitLink,'TTYPE.F']);
  IdexA:=-1;
  IdexA:=FListNode.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    CachNode:=nil;
    CachNode:=TGkxCachNode(FListNode.Objects[IdexA]);
    if CachNode<>nil then
    begin
      if IsForbit then
      begin
        CachNode.UpdateTypF(AUniConnection,IsForbit,AUnitLink);
      end;
    end;
  end else
  begin
    CachNode:=TGkxCachNode.Create;
    CachNode.UpdateTypF(AUniConnection,IsForbit,AUnitLink);
    FListNode.AddObject(TextA,CachNode);
  end;
end;
{ TGkxCachNode }

constructor TGkxCachNode.Create;
begin
  FListData:=nil;
end;

destructor TGkxCachNode.Destroy;
begin
  if FListData<>nil then TKzUtils.TryFreeAndNil(FListData);
  inherited;
end;



function  TGkxCachNode.GetObjtCord(AIdex: Integer; IsCreate: Boolean;
  AUnitLink: string):TCORD;
var
  IdexA:Integer;
  TextA:string;
begin
  Result:=nil;
  if (FListData=nil) or (FListData.Count=0) then Exit;

  IdexA:=-1;
  TextA:=Format('%S-%D',[AUnitLink,AIdex]);
  IdexA:=FListData.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    if not IsCreate then
    begin
      Result:=TCORD(FListData.Objects[IdexA]);
    end else
    begin
      Result:=TCORD.CopyIt(    TCORD(FListData.Objects[IdexA])   );
    end;
  end;
end;

function TGkxCachNode.GetObjtJGFL(AFLID: Integer; IsCreate: Boolean;
  AUnitLink: string; AGZJGIDEX: Integer): TGKJGFL;
var
  IdexA:Integer;
  TextA:string;
begin
  Result:=nil;
  if (FListData=nil) or (FListData.Count=0) then Exit;

  IdexA:=-1;
  TextA:=Format('%S-%D-%D',[AUnitLink,AGZJGIDEX,AFLID]);
  IdexA:=FListData.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    if not IsCreate then
    begin
      Result:=TGKJGFL(FListData.Objects[IdexA]);
    end else
    begin
      Result:=TGKJGFL.CopyIt(    TGKJGFL(FListData.Objects[IdexA])   );
    end;
  end;
end;

function TGkxCachNode.GetObjtType(AIdex: Integer; AType: string;
  IsCreate: Boolean; AUnitLink: string): TTYPE;
var
  IdexA:Integer;
  TextA:string;
begin
  Result:=nil;
  if (FListData=nil) or (FListData.Count=0) then Exit;

  IdexA:=-1;
  TextA:=Format('%S-%S-%D',[AUnitLink,AType,AIdex]);
  IdexA:=FListData.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    if not IsCreate then
    begin
      Result:=TTYPE(FListData.Objects[IdexA]);
    end else
    begin
      Result:=TTYPE.CopyIt(    TTYPE(FListData.Objects[IdexA])   );
    end;
  end;
end;

function TGkxCachNode.GetObjtTypF(AText:string;AType:Integer;
  IsCreate: Boolean; AUnitLink: string): TTYPE;
var
  IdexA:Integer;
  TextA:string;
begin
  Result:=nil;
  if (FListData=nil) or (FListData.Count=0) then Exit;

  IdexA:=-1;
  TextA:=Format('%S-%D-%S',[AUnitLink,AType,AText]);
  IdexA:=FListData.IndexOf(TextA);
  if IdexA<>-1 then
  begin
    if not IsCreate then
    begin
      Result:=TTYPE(FListData.Objects[IdexA]);
    end else
    begin
      Result:=TTYPE.CopyIt(    TTYPE(FListData.Objects[IdexA])   );
    end;
  end;
end;

procedure TGkxCachNode.UpdateCord(AUniConnection: TUniConnection;
  IsForbit: Boolean; AUnitLink: string);
var
  SQLA:string;
begin
  SQLA:='SELECT * FROM GKX_CORD ORDER BY UNIT_LINK,CORD_IDEX';
  if FListData=nil then
  begin
    FListData:=THashedStringList.Create;
  end;
  TKzUtils.JustCleanList(FListData);
  TCord.ListDB(SQLA,['UNIT_LINK','CORD_IDEX'],AUniConnection,TStringList(FListData));
end;

procedure TGkxCachNode.UpdateJGFL(AUniConnection: TUniConnection;
  IsForbit: Boolean; AUnitLink: string);
var
  SQLA:string;
begin
  SQLA:=TGKJGFL.ExpSQL_IN_ALL('-1',1);
  if FListData=nil then
  begin
    FListData:=THashedStringList.Create;
  end;
  TKzUtils.JustCleanList(FListData);
  TGKJGFL.ListDB(SQLA,['UNIT_LINK','GKJG_IDEX','GKJG_FLID'],AUniConnection,TStringList(FListData));
end;

procedure TGkxCachNode.UpdateType(AUniConnection: TUniConnection;
  IsForbit: Boolean; AUnitLink: string);
var
  SQLA:string;
begin
  SQLA:=TTYPE.ExpSQL;
  if FListData=nil then
  begin
    FListData:=THashedStringList.Create;
  end;
  TKzUtils.JustCleanList(FListData);
  TTYPE.ListDB(SQLA,['UNIT_LINK','CORD_TYPE','TYPE_IDEX'],AUniConnection,TStringList(FListData));
end;

procedure TGkxCachNode.UpdateTypF(AUniConnection: TUniConnection;
  IsForbit: Boolean; AUnitLink: string);
var
  SQLA:string;
begin
  SQLA:=TTYPE.ExpSQL;
  if FListData=nil then
  begin
    FListData:=THashedStringList.Create;
  end;
  TKzUtils.JustCleanList(FListData);
  TTYPE.ListDB(SQLA,['UNIT_LINK','CORD_IDEX','TYPE_NAME'],AUniConnection,TStringList(FListData));
end;

initialization
begin
  GkxContxtEx:=TGkxContxt.Create;
end;

finalization
begin
  if GkxContxtEx<>nil then FreeAndNil(GkxContxtEx);
end;

end.
